home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xless / xless141.z / xless141 / xless-1.4.1 / util.c < prev    next >
C/C++ Source or Header  |  1993-05-05  |  8KB  |  315 lines

  1. /*
  2.  * Copyright 1989 Massachusetts Institute of Technology
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of M.I.T. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  M.I.T. makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  15.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  16.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  18.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  19.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20.  *
  21.  * Author: Carlo Lisa
  22.  *       MIT Project Athena
  23.  *       carlo@athena.mit.edu
  24.  *
  25.  * $Header: /usr/sww/share/src/X11R5/local/clients/xless-1.4/RCS/util.c,v 1.12 1993/02/26 01:13:12 dglo Exp dglo $
  26.  */
  27.  
  28. #include "xless.h"
  29.  
  30. XFontStruct *buttonfont;
  31. XFontStruct *labelfont;
  32. XFontStruct *textfont;
  33. Cursor stdcur;
  34. Cursor dialogcur;
  35.  
  36. static XawTextPosition Find ARGS((const char *, const char *));
  37.  
  38. static struct opendata {
  39.   Widget errbox;
  40.   char *errmsg;
  41. } data;
  42.  
  43. /*
  44.  *    Function name:    SetPopup
  45.  *    Description:    This function pops up the specified dialog box.
  46.  *    Arguments:    top - where to pop up this dialog box
  47.  *                    wdg - the widget to be popped up.
  48.  *    Returns:    None.
  49.  */
  50.  
  51. void
  52. SetPopup(top, wdg)
  53. Widget top, wdg;
  54. {
  55.   Position x=0, y=0;
  56.   Arg args[5];
  57.   Window rwin;
  58.   Window chwin;
  59.   int rx, ry, wx, wy;
  60.   Dimension wd = 0;
  61.   Dimension he = 0;
  62.   unsigned int mask;
  63.  
  64.   /* Make the popup shell "wdg" come up at the current pointer position */
  65.   XQueryPointer(XtDisplay(top), XtWindow(top), &rwin, &chwin, &rx, &ry,
  66.         &wx, &wy, &mask);
  67.  
  68.   XtSetArg(args[0], XtNwidth, &wd);
  69.   XtSetArg(args[1], XtNheight, &he);
  70.   XtGetValues(wdg, args, (Cardinal)2);
  71.   if (wd == 0) wd = DIALOGWIDTH;
  72.   if (he == 0) he = DIALOGHEIGHT;
  73.  
  74.   x = rx - wd/2;
  75.   y = ry - he/2;
  76.  
  77.   XtSetArg(args[0], XtNx, x);
  78.   XtSetArg(args[1], XtNy, y);
  79.   XtSetValues(wdg, args, (Cardinal)2);
  80.  
  81.   /* Popup the widget */
  82.   XtPopup(wdg, XtGrabExclusive);
  83. }
  84.  
  85. /*
  86.  *    Function Name: Find
  87.  *    Description:   This function search the specified string in
  88.  *               the specified chunk of memory.
  89.  *    Arguments:     data   - a pointer to the data read from the file
  90.  *                    and stored in dynamic memory.
  91.  *               string - a pointer to the string to search.
  92.  *    Returns:       #char offset from the current cursor position
  93.  *               to the beginning of string;
  94.  *               -1 if string not found.
  95.  */
  96.  
  97. static XawTextPosition
  98. Find(data, string)
  99. const char *data;
  100. const char *string;
  101. {
  102.   const char *tmpd, *tmps;
  103.   XawTextPosition beg = 0;
  104.  
  105.   tmpd = data;
  106.   tmps = string;
  107.  
  108.   while ((*tmpd != '\0')&&(*tmps != '\0')) {
  109.     if (*tmpd == *tmps) {
  110.       tmpd++;
  111.       tmps++;
  112.     } else {
  113.       tmpd++;
  114.       tmps = string;
  115.     }
  116.   }
  117.   if (*tmps == '\0') {
  118.     beg = tmpd - data - strlen(string);
  119.     return(beg);
  120.   }
  121.   else
  122.     return ((XawTextPosition)(-1));
  123. }
  124.  
  125. /*
  126.  *    Function name:    DoSearch
  127.  *    Description:    This function executes the search for the
  128.  *            specified string.
  129.  *    Arguments:    tex       - the widget to be popped up.
  130.  *            data      - pointer to memory containing the file.
  131.  *            sele      - string to search for.
  132.  *            parent    - the parent widget.
  133.  *    Returns:    None.
  134.  */
  135.  
  136. void
  137. DoSearch(tex, data, sele, parent)
  138. Widget tex;
  139. const char *data;
  140. const char *sele;
  141. Widget parent;
  142. {
  143.   XawTextPosition beg, end, offset1, offset2;
  144.   int len;
  145.   const char *search_buf;
  146.   static Widget notFound = 0;
  147.  
  148.   len = strlen(sele);
  149.   offset1 = XawTextGetInsertionPoint(tex);
  150.   search_buf = data + offset1;
  151.   if ((offset2 = Find(search_buf, sele)) != -1) {
  152.     beg = offset1 + offset2;
  153.     end = beg + len;
  154.     XawTextSetInsertionPoint(tex, beg+len);
  155.     XawTextSetSelection(tex, beg, end);
  156.   } else {
  157.     if (!notFound)
  158.       notFound = MessageBox(parent, "String not found .....", "OK", 0, 0);
  159.     if (notFound)
  160.       SetPopup(parent, notFound);
  161.   }
  162. }
  163.  
  164. /*
  165.  *    Function name:    CheckFonts
  166.  *    Description:    This function checks the resource DB for the
  167.  *            user specified fonts.
  168.  *    Arguments:    None.
  169.  *    Returns:    None.
  170.  */
  171.  
  172. void
  173. CheckFonts()
  174. {
  175.   if (!(buttonfont = resources.fonts.button))
  176.     buttonfont = resources.fonts.standard;
  177.   if (!(labelfont = resources.fonts.label))
  178.     labelfont = resources.fonts.standard;
  179.   if (!(textfont = resources.fonts.text))
  180.     textfont = resources.fonts.standard;
  181.  
  182.   dialogcur = resources.cursors.dialog;
  183.   stdcur = resources.cursors.dialog;
  184.  
  185.   if (!buttonfont || !labelfont || !textfont) {
  186.     fprintf(stderr, "xless: unable to open any of the specified fonts\n");
  187.     exit(1);
  188.   }
  189. }
  190.  
  191. /*
  192.  *    Function name:    SetReadText
  193.  *    Description:    This function set a new text in a
  194.  *            read-only text widget.
  195.  *    Arguments:    w      - the asciiText widget.
  196.  *            string - the new text source.
  197.  *    Returns:    None.
  198.  */
  199.  
  200. void
  201. SetReadText(w, string)
  202. Widget w;
  203. char *string;
  204. {
  205.   Arg arg[2];
  206.   Widget source;
  207.  
  208.   /* Create a new source for the text widget, and put in the new string */
  209.   XtSetArg(arg[0], XtNstring, string);
  210.   XtSetArg(arg[1], XtNeditType, XawtextRead);
  211.   source = XtCreateWidget("Read_Text", asciiSrcObjectClass, w,
  212.               arg, 2);
  213.   XawTextSetSource(w, source, (XawTextPosition) 0);
  214. }
  215.  
  216. static void
  217. popdownCouldntOpen(widget, closure, callData)
  218. Widget widget;
  219. XtPointer closure;
  220. XtPointer callData;
  221. {
  222.   XtPopdown(data.errbox);
  223.   XtDestroyWidget(data.errbox);
  224.   free(data.errmsg);
  225. }
  226.  
  227. /*
  228.  *    Function name:    CouldntOpen
  229.  *    Description:    This function pops up the "Couldn't open"  dialog box.
  230.  *    Arguments:    wdg - the widget to be popped up on.
  231.  *    Returns:    None.
  232.  */
  233.  
  234. void
  235. CouldntOpen(top, filename)
  236. Widget top;
  237. const char *filename;
  238. {
  239.   const char *msgpart = "Couldn't open file: ";
  240.  
  241.   if (data.errmsg = (char *)malloc(strlen(msgpart) + strlen(filename) + 1)) {
  242.     strcpy(data.errmsg, msgpart);
  243.     strcat(data.errmsg, filename);
  244.  
  245.     data.errbox = MessageBox(top, data.errmsg, "OK", popdownCouldntOpen, 0);
  246.     if (data.errbox)
  247.       SetPopup(top, data.errbox);
  248.   }
  249. }
  250.  
  251. #ifdef TILDE_EXPANSION
  252.  
  253. #include <pwd.h>
  254.  
  255. #define USERNAMELEN    9
  256.  
  257. const char *
  258. TildeExpand(filename)
  259. const char *filename;
  260. {
  261.   struct passwd *pw;
  262.   char username[USERNAMELEN], *bptr = username;
  263.   const char *end;
  264.   int len;
  265.  
  266.   /* find end of tilde'd name */
  267.   end = strchr(filename, '/');
  268.   if (!end)
  269.     end = filename + strlen(filename);
  270.  
  271.   /* if it's just '~/...' or '~' */
  272.   if (end == filename + 1) {
  273.  
  274.     /* look up this UID in passwd file */
  275.     pw = getpwuid(getuid());
  276.  
  277.   } else {
  278.  
  279.     /* allocate a buffer if static one is too small */
  280.     len = end - filename;
  281.     if (len > USERNAMELEN)
  282.       bptr = (char *)malloc(len);
  283.  
  284.     /* copy name into buffer */
  285.     len--;
  286.     strncpy(bptr, filename + 1, len);
  287.     bptr[len] = 0;
  288.  
  289.     /* look up this user in passwd file */
  290.     pw = getpwnam(bptr);
  291.  
  292.     /* free allocated memory */
  293.     if (bptr != username)
  294.       free(bptr);
  295.   }
  296.  
  297.   /* if we found a passwd entry... */
  298.   if (pw) {
  299.  
  300.     /* get enough memory for expanded string */
  301.     bptr = (char *)malloc(strlen(pw->pw_dir) + strlen(end) + 1);
  302.  
  303.     /* create new string */
  304.     if (bptr) {
  305.       strcpy(bptr, pw->pw_dir);
  306.       strcat(bptr, end);
  307.       filename = bptr;
  308.     }
  309.   }
  310.  
  311.   /* return final string */
  312.   return(filename);
  313. }
  314. #endif /* TILDE_EXPANSION */
  315.